<template>
  <div class="details-container">
    <el-card>
      <el-tabs v-model="activeName" class="form-list">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form
            ref="depForm"
            :model="accountData"
            :rules="rules"
            label-width="80px"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="accountData.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="accountData.password"
                type="password"
                placeholder="请输入要修改的密码"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="successForm">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <info />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 导入api接口
import { getStaffInfo } from '@/api/user'
import { alterLogin } from '@/api/employees'
import Info from './Info.vue'
import Job from './Job.vue'
import { mapGetters, mapActions } from 'vuex'
export default {
  components: { Info, Job },
  data() {
    return {
      id: this.$route.params.id,
      activeName: 'first',
      // 存储账户数据
      accountData: {
        username: '',
        password: ''
      },
      // 表单校验
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在2~4个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在6~16个字符', trigger: 'blur' }
        ]
      }
    }
  },
  //  页面一打开调用抽取的接口获取员工信息
  async created() {
    const res = await getStaffInfo({ id: this.id })
    console.log(res)
    this.accountData = res
    this.accountData.password = ''
  },
  methods: {
    ...mapActions('user', ['loginOut']),
    successForm() {
      this.$refs.depForm.validate(async v => {
        if (v) {
          await alterLogin({ ...this.accountData })
          if (this.id === this.userInfo.userId) {
            this.$message.success('保存成功,请重新登录')
            this.loginOut()
            this.$router.push(`/login?redirect=${this.$route.fullPath}`)
          } else {
            this.$message.success('保存成功')
          }

          this.accountData.password = ''
          return true
        } else {
          return false
        }
      })
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  }
}
</script>

<style lang="scss">
.details-container {
  padding: 20px;
  #pane-first {
    padding: 20px 200px;
    .el-form-item__content {
      width: 350px;
    }
  }
}
</style>
